package com.budwk.app.sqfwV2.generator.lanhuadu.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BasePdfParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.component.SignImageComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.enums.lanHuaDu.LanHuaDuFileServiceEnum;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.UrlImageInfo;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工作汇报，版本号1.0
 */

@Slf4j
public class lhdWorkReportDocGenerator extends BaseTemplatePdfGenerator{

    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        if (pdfDTO.isDownTemplate()) {
            return true;
        }
        return true;
    }

    @Override
    public String getTemplateName() {
        return LanHuaDuFileServiceEnum.C.getFilePath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfDTO) {
        if (pdfDTO.isDownTemplate()) {  // 下载模板
            buildNullData(paramMap);
            return;
        }
        BasePdfParamDTO basePdfParam = pdfDTO.getBasePdfParamDTO();
        // 1 构建基本数据
        paramMap.put("district", StrUtil.formatZhiShu(basePdfParam.getDistrict()));
        paramMap.put("county", StrUtil.formatZhiShu(basePdfParam.getCounty()));
        paramMap.put("community", StrUtil.formatZhiShu(basePdfParam.getCommunity()));
        paramMap.put("downTime", DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN));
        // 2 构建检查日期和走防图片
        bulidHistroy(paramMap, basePdfParam);
    }

    private void bulidHistroy(Map<String, Object> paramMap, BasePdfParamDTO basePdfParam) {
        List<String> csImage = new ArrayList<>();// 场所图片
        // 按照场所查询所有之前填写的信息
        SqfwFormsummaryNewService formsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        List<Sqfw_formSummary_new> summaryNewsHis = formsummaryNewService.queryBySqfwIdAndType(basePdfParam.getSqfwId(), SupportBusinessTypeEnum.SAFETY_CHECK.getName());
        // 取出当前场所填写的所有信息
        for (Sqfw_formSummary_new summaryNewsHi : summaryNewsHis) {
            if (csImage.size() < 6) {
                FireSafetyOnDayingRoadPdfDTO pdfDTO = JSONObject.parseObject(summaryNewsHi.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
                String visitPictures1 = pdfDTO.getVisitPictures();
                if (StringUtils.isNotEmpty(visitPictures1)) {
                    csImage.add(visitPictures1.split(",")[0]);
                }
            }
        }
        String startTime = null;
        if (summaryNewsHis.size() > 1) {
            Sqfw_formSummary_new summaryNew = summaryNewsHis.get(summaryNewsHis.size() - 1);
            startTime = DateUtil.format(summaryNew.getFillDate(), DatePattern.CHINESE_DATE_PATTERN);
        }
        String endTime = DateUtil.format(basePdfParam.getFillDate(), DatePattern.CHINESE_DATE_PATTERN);
        signVisImage(paramMap, csImage);
        // 检查日期
        if (StringUtils.isNotEmpty(startTime)) {
            paramMap.put("checkDate", startTime + "-" + endTime);
        } else {
            paramMap.put("checkDate", endTime);
        }
    }

    // 走防图片
    public static void signVisImage(Map<String, Object> map, List<String> imageUrl) {
        String[] items = {"A", "B", "C", "D", "E", "F"};
        for (String item : items) {
            map.put(item, null);
        }
        if (imageUrl.size() > 0) {
            List<PictureRenderData> list = addWordPicture(imageUrl);
            for (int i = 0; i < list.size(); i++) {
                map.put(items[i], list.get(i));
            }
        }
    }

    public static List<PictureRenderData> addWordPicture(List<String> imageUrlList) {
        List<PictureRenderData> list = Lists.newArrayList();
        for (String imageUrl : imageUrlList) {
            UrlImageInfo urlImageInfo = SignImageComponent.getImageUrl(imageUrl, 5, 5);
            PictureRenderData pictureRenderData = Pictures.ofBytes(urlImageInfo.getBytes()).sizeInCm(urlImageInfo.getTargetWidth(), urlImageInfo.getTargetHeight()).create();
            list.add(pictureRenderData);
        }
        return list;
    }

    /**
     * 构建空模板数据
     * @param paramMap
     */
    private void buildNullData(Map<String, Object> paramMap) {
        paramMap.put("county", "XX街道");
        paramMap.put("downTime", DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN));
        String[] items = {"A", "B", "C", "D", "E", "F"};
        for (String item : items) {
            paramMap.put(item, null);
        }
        paramMap.put("community", "XX社区");
        paramMap.put("checkDate", "xxxx年xx月xx日");
    }
}
